package com.xbn.xbnmall.product.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbn.common.utils.PageUtils;
import com.xbn.common.utils.Query;

import com.xbn.xbnmall.product.dao.BrandDao;
import com.xbn.xbnmall.product.entity.BrandEntity;
import com.xbn.xbnmall.product.service.BrandService;
import org.springframework.transaction.annotation.Transactional;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationServiceImpl categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        //如果前端有查询参数key，获取过来进行模糊查询
        if(params.containsKey("key"))
        {
            String key= (String) params.get("key");
            wrapper.eq("brand_id",key).or().like("name",key);
        }
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),wrapper

        );

        return new PageUtils(page);
    }

    /*
    * 更新的时候要更新其它所有冗余存储的数据
    * 保证冗余字段的数据一致
    * */
    @Transactional
    @Override
    public void updateDetail(BrandEntity brand) {
    this.updateById(brand);
    //如果品牌名不为空，我们就需要同时去修改其它表的品牌名冗余字段
    if(!StringUtils.isEmpty(brand.getName()))
    {
        //TODO  Brand相关更新，所有品牌相关冗余需要更新
        categoryBrandRelationService.updateBrandName(brand.getBrandId(),brand.getName());

    }
    }

    @Override
    public List<BrandEntity> getBrandByIds(List<Long> brandIds) {
        return    baseMapper.selectBatchIds(brandIds);
    }

}